/**
 * 
 */
package co.edu.uniandes.mdd.project1.sample.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import co.edu.uniandes.mdd.project1.sample.controller.Controller;

/**
 * @author Leonardo
 * 
 */
public class PanelController extends JPanel /* implements KeyListener */{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton buttonArriba;
	private JButton buttonAbajo;
	private JButton buttonDerecha;
	private JButton buttonIzquierda;

	//private final Controller controller;

	/**
	 * 
	 */
	public PanelController(final Controller controller) {
		//this.controller = controller;

		setBorder(new CompoundBorder(new EmptyBorder(0, 0, 5, 0),
				new TitledBorder("Controles")));
		setLayout(new GridLayout(1, 5));

		// Se crean los botones por cada comportamiento controlado creado

		buttonArriba = new JButton("Arriba");
		buttonArriba.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.movePlayerArriba();

			}
		});
		buttonAbajo = new JButton("Abajo");
		buttonAbajo.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				controller.movePlayerAbajo();
			}
		});

		buttonDerecha = new JButton("Derecha");
		buttonDerecha.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				controller.movePlayerDerecha();
			}
		});

		buttonIzquierda = new JButton("Izquierda");
		buttonIzquierda.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				controller.movePlayerIzquierda();
			}
		});

		add(buttonArriba);
		add(buttonAbajo);
		add(buttonDerecha);
		add(buttonIzquierda);
		
		KeyAdapter a = new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				String keyText = KeyEvent.getKeyText(e.getKeyCode());
				if (keyText.equals("A")) {
					controller.movePlayerIzquierda();
				} else if (keyText.equals("D")) {
					controller.movePlayerDerecha();
				} else if (keyText.equals("W")) {
					controller.movePlayerArriba();
				} else if (keyText.equals("S")) {
					controller.movePlayerAbajo();
				}
			}
		};
		this.buttonAbajo.addKeyListener(a);
		this.buttonArriba.addKeyListener(a);
		this.buttonDerecha.addKeyListener(a);
		this.buttonIzquierda.addKeyListener(a);
		
	}

}
